<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div >
    <!--表单-->
    <form class="layui-form " lay-filter="accreditUpdateQuotationForm" style="text-align: center">
        <div class="layui-block">
            <div class="layui-inline">
                <label class="layui-form-label">申请人</label>
                <div class="layui-input-inline">
                    <input class="layui-input" type="tel" name="userName">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">报告单据</label>
                <div class="layui-input-inline">
                    <input class="layui-input" type="tel" name="costReportId">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">成品编码</label>
                <div class="layui-input-inline">
                    <input  class="layui-input" type="tel" name="finishedProductCode">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">公司账套</label>
                <div class="layui-input-inline" style="width:260px" >
                    <select  name="pkOrgAccount"  lay-filter="accreditUpdateQuotationPkOrg" id="accreditUpdatePkOrg">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <button type="submit" class="layui-btn  layui-btn-normal" lay-submit="" lay-filter="accreditUpdateQuotationQueryBut">查询</button>
                </div>
            </div>
        </div>
    </form>
    <!--表格-->
    <div style="margin: 0 auto">
        <table id="accreditUpdateQuotationTable" lay-filter="accreditUpdateQuotationTable"></table>
    </div>
    <!-- 树 -->
</div>
<script type="text/html" id="toolbarDemo" >
    <div class="layui-btn-container" style="width:300px;height:20px;margin: 0 auto">
        <button class="layui-btn layui-btn-sm" lay-event="add">授权</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>
<script th:inline="none">

    layui.use(['form','table','layer'],function () {
        var form = layui.form;
       var table= layui.table;
       var layer= layui.layer;
        //为公司账套赋值
        accreditUpdatePkOrgInit();

        //监听submit按钮
        form.on('submit(accreditUpdateQuotationQueryBut)',function (data) {

            accreditUpdateQuotationTableInit(table,data.field);
            return false;
        })

        //监听公司账套
        // form.on('select(accreditUpdateQuotationPkOrg)',function (data) {
        //     //console.log(data.elem); //得到select原始DOM对象
        //    // console.log(data.value); //得到被选中的值
        //     //console.log(data.othis); //得到美化后的DOM对象
        //     // 表格的初始化
        //     //根据值刷新表格
        //     accreditUpdateQuotationTableInit(table,data.value);
        // })

        //监听行双击事件
        table.on('rowDouble(accreditUpdateQuotationTable)', function(obj){
            var data=obj.data;
            //弹出框
            layer.open({
                title:"授权-成本单据["+data.cost_report_id+"]",
                type: 1,
                area: ['300px', '500px'],
                content: '<div id="accreditUpdateQuotationTreeDiv"> <table id="accreditUpdateQuotationDialogUserTable"></table></div>'
                ,btn: ['授权']
                ,yes: function(index, layero){
                    //获取成本报告表双击行的数据
                    //获取用户表格中选中的数据
                    var user = table.checkStatus('accreditUpdateQuotationDialogUserTableId'); //即为基础参数 id 对应的值
                    //修改：costReport
                   var rdata=commonAjax('get','/costReport/updateSystemAppointUserId',{"userId":user.data[0].userId,"costReportId":data.cost_report_id})
                    layer.msg(rdata.msg);
                    if(rdata.status==200){
                        //重新加载成本报告表的数据
                        table.reload('accreditUpdateQuotationTableId', {
                            // where: { //设定异步数据接口的额外参数，任意设
                            //     pkOrgAccount: '0001O110000000001HET'
                            // }
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.closeAll('page'); //关闭所有的page页面
                    }else {
                        return false;
                    }

                }
                ,cancel: function(){
                    //右上角关闭回调

                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });

            //加载窗口用户的table
            accreditUpdateQuotationDialogUserTableInit(table,'');

        });

        form.render();//动态加载的页面需要添加此方法进行再次渲染
    })

    //初始化公司账套的组织
    function accreditUpdatePkOrgInit() {
        var orgs = commonAjax("get","/manage/user/selectallhrorg");
        $.each(orgs,function (index,item) {
            $("#accreditUpdatePkOrg").append(new Option(item.name,item.pkhrorg))
        })
    }

    //设置成本报告表格父元素的宽高
    $("#accreditUpdateQuotationTable").parent().css({"width":costsystemFblW,"height":costsystemFblH})
    //成本报告表格数据获取
    function accreditUpdateQuotationTableInit(table,data) {
        table.render({
            id: 'accreditUpdateQuotationTableId',
            page:true,
            elem: '#accreditUpdateQuotationTable' //指定原始表格元素选择器
            ,width:costsystemFblW
           ,height: costsystemFblH //容器高度
            ,url:'/costReport/selectAccreditUpdateQuotation?pkOrgAccount='+data.pkOrgAccount+"&userName="+data.userName+"&costReportId="+data.costReportId+"&finishedProductCode="+data.finishedProductCode
            ,toolbar:true
            ,defaultToolbar:[{
                title: '授权' //标题
                ,layEvent: 'LAYTABLE_TIPS' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-tips' //图标类名
            }]
            ,cols: [[ //表头
                {field: 'user_id', title: '申请人Id', width:180,align:'center',hide:true}
                ,{field: 'appoint_user_id', title: '授权人Id', width:180,align:'center',hide:true}
                ,{field: 'user_name', title: '申请人', width:180,align:'center'}
                ,{field: 'user_code', title: '工号', width:180,align:'center'}
                ,{field: 'cost_report_id', title: '成本报告单据号',align:'center', width:180}
                ,{field: 'finished_product_code', title: '成本编码', width:180,align:'center'}
                ,{field: 'cost_report_date', title: '日期', width:180,align:'center', sort: true}
                ,{field: 'gross_margin_money', title: '毛利', width:180,align:'center'}
                ,{field: 'aname', title: '已授权人', width:180,align:'center',templet:function (row) {
                    if (!row.aname){
                        return '无';
                    }
                        return "<span style='color: #CC2222'>"+row.aname+"</span>"
                    }}

            ]]
        });
    }

    //用户表格
    function accreditUpdateQuotationDialogUserTableInit(table,userName) {
        console.log(userName)
        if (!userName){
            userName=''
        }
        table.render({
            id: 'accreditUpdateQuotationDialogUserTableId',
            elem: '#accreditUpdateQuotationDialogUserTable' //指定原始表格元素选择器
            ,url:'/manage/user/selectAllUserLayuiTable?username='+userName
            ,cols: [[ //表头
                {type: 'radio', fixed: 'left'}
                ,{field: 'userId', title: '申请人Id', width:180,align:'center',hide:true}
                ,{field: 'userName', title: '姓名', width:110,align:'center'}
                ,{field: 'userCode', title: '工号', width:120,align:'center'}
            ]]
        });
    }
</script>
</body>
</html>